﻿using Microsoft.VisualStudio.Modeling;
using System.Collections.Generic;
using System.Data;

namespace Project.Co_DataTransformationPrototype
{
    
    public static partial class Operators
    {
        public partial class Operator
        {
            public Operator UpdateAttributeProperty<T>(System.Guid propertyID, IList<EntityAttribute> attributeList, T newValue, Entity foreignEntityReference = null)
            {
                foreach (EntityAttribute attr in attributeList)
                    UpdateAttributeProperty(propertyID, attr, newValue, foreignEntityReference);

                return this;
            }

            public Operator UpdateAttributeProperty<T>(System.Guid propertyID, EntityAttribute currentAttribute, T newValue, Entity foreignEntityReference = null, string oldEntityName = null, string oldAttributeName = null)
            {
                // Transaction is required if you want to update elements.
                using (Transaction t = opStore.TransactionManager.BeginTransaction("Update Attribute Property Value"))
                {
                    if (propertyID.Equals(EntityAttribute.isPrimaryKeyDomainPropertyId))
                    {
                        bool value;
                        bool.TryParse(newValue.ToString(), out value);
                        currentAttribute.isPrimaryKey = value;
                        
                        // Corrective context - facilitate attribute name change propagation to PK constraints
                        if (oldAttributeName != null)
                            t.Context.Add(GetPrimaryKeyRefTransactionKey, oldAttributeName);

                        t.Commit();
                    }
                    else if (propertyID.Equals(EntityAttribute.isForeignKeyDomainPropertyId))
                    {
                        bool value;
                        bool.TryParse(newValue.ToString(), out value);
                                                
                        if (value == true)
                            currentAttribute.ReferencedEntityID = foreignEntityReference.Id;
                        //else { currentAttribute.ReferencedEntityID = System.Guid.Empty; }
                        currentAttribute.isForeignKey = value;
                        t.Context.Add(GetForeignKeyRefTransactionKey, foreignEntityReference);

                        // Corrective context - facilitate entity-name & attribute-name change propagation to FK constraints
                        if (oldEntityName != null)
                            t.Context.Add(GetFKEntityOldNameTransactionKey, oldEntityName);
                        else if (oldAttributeName != null)
                            t.Context.Add(GetFKAttributeOldNameTransactionKey, oldAttributeName);

                        t.Commit();
                    }
                }

                return this;
            }

            public Operator UpdateAttributeProperty(EntityAttribute currentAttribute, SqlDbType newValue, SqlDbType oldValue)
            {
                // Transaction is required if you want to update elements.
                using (Transaction t = opStore.TransactionManager.BeginTransaction("Update Attribute's Type"))
                {
                    currentAttribute.Type = newValue;
                    t.Commit();
                }

                return this;
            }
        }
    }

}
